package com.aisino.controller;

import com.aisino.entity.TemplateInputOutDto;
import com.aisino.entity.User;
import com.aisino.listener.TemplateInputOutListener;
import com.aisino.service.PgsqlService;
import com.aisino.utils.POIUtils;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController()
@RequestMapping("pgsql")
public class PgsqlController {
    @Autowired
    private PgsqlService pgsqlService;

    @PostMapping("demo1")
    public String demo1(String msg){
        return "hello,demo1!!!!!!" + msg;
    }

    @GetMapping("getUsers")
    public List<User> getUsers(){
        return pgsqlService.getUsers();
    }

    /*=============================调用示例=========================================*/
    //导出
    @RequestMapping("exportList")
    public void exportList(HttpServletResponse response, String ids) {
        // String fileName = "人员档案列表";
        // List<User> users = sRPService.exportList(ids);
        // 列名
        // String columnNames[] = {"ID", "姓名", "性别", "所属部门", "所属单位", "邮箱",
        //         "电话", "手机", "学历/学位", "专业/专科方向", "直属上级", "账号锁定"};
        // map中的key
        // String keys[] = {"id", "userName", "gender", "dept", "unit", "email",
        //         "tel", "phone", "degree", "major", "parentName", "isLocked"};

        String fileName = "用户表";
        List<User> users = pgsqlService.getUsers();

        String[] columnNames = {"ID", "姓名", "分数", "国家", "创建时间"};
        String[] keys = {"id", "player", "score", "team", "createTime"};
        try {
            POIUtils.start_download(response, fileName, users, columnNames, keys);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 假设你有一个Date对象
        Date date = new Date(); // 这里只是创建一个示例Date对象，你可以替换为你自己的Date对象

        // 创建一个SimpleDateFormat对象并指定你想要的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 使用SimpleDateFormat对象来格式化Date
        String formattedDate = sdf.format(date);

        //将字符串转换成时间对象
        Date parse = null;
        try {
            parse = sdf.parse(formattedDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        System.out.println("parse = " + parse.toString());

        // 输出格式化后的日期
        System.out.println(formattedDate);
    }
    //导入
   @RequestMapping("readUserExcel")
   public void readUserExcel(MultipartFile multipartFile, HttpServletRequest request) {
        try {
            List<String[]> readExcel = POIUtils.readExcel(multipartFile);
            System.out.println("readExcel = " + readExcel);
            System.out.println("readExcel.size() = "+ readExcel.size());
            for (int i = 0; i < readExcel.size(); i++){
                System.out.println(Arrays.toString(readExcel.get(i)));
            }

            System.out.println(multipartFile.getOriginalFilename());//用户表 (4).xlsx
            System.out.println(multipartFile.getName());//multipartFile
            System.out.println(multipartFile.getSize());//4237 b单位

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("readByEasyExcel")
    public void readUserExcel(MultipartFile multipartFile) {
        try {
            EasyExcel.read( multipartFile.getInputStream(), TemplateInputOutDto.class,new TemplateInputOutListener()).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
